<?php
class calendar_model extends CI_Model {
	
	//创建日程
	function create_calendar(){
		$cal_time="";
		if($_POST['time'])
		{
			try 
			{
				$time=$_POST['time'];
				$year=substr($time,0,4);
				$month=substr($time,5,2);
				$day=substr($time,8,2);
				$hour=substr($time,11,2);
				$minute=substr($time,14,2);
				$second="00";
				$cal_time=mktime($hour,$minute,$second,$month,$day,$year);
			}
			catch (Exception $e){
				echo $e;
			}
		}
		
		//判断是否时间重复
		$user_id=$this->session->userdata('user_id');
		$this->db->where('user_id',$user_id);
		$this->db->where('cal_time',date('Y-m-d H:i:s',$cal_time));
		$query = $this->db->get('calendar');
		

		if ($query->num_rows>0){
			return false;
		}
		
		$type=$_POST['type'];
		$title=$_POST['title'];
		$location_address=$_POST['location_address'];
		$location_x=doubleval($_POST['location_x']);		
		$location_y=doubleval($_POST['location_y']);
		
		$new_calendar_insert_data=array(
			'user_id'=>$this->session->userdata('user_id'),
			'cal_time'=>date('Y-m-d H:i:s',$cal_time),
			'point_x'=>$location_x,
			'point_y'=>$location_y,
			'point_title'=>$title,
			'point_addr'=>$location_address,
			'type'=>$type
		);
		$insert = $this->db->insert('calendar', $new_calendar_insert_data);
		return $insert;
	}
	
	//获取未来的日程
	function get_future_cal(){
		date_default_timezone_set('Asia/Chongqing');
		
		$user_id=$this->session->userdata('user_id');
		$this->db->where('user_id',$user_id);
		$this->db->where('unix_timestamp(cal_time)>',time());
		$this->db->where('active',1);
		$this->db->order_by('cal_time','desc');
		
		$query = $this->db->get('calendar');
		
		
		return $query->result();
		
	}
	
	function delete_cal_by_id($id){
		$result=$this->db->delete('calendar', array('id' => $id));
		return $result; 
	}
	
	function get_cal_by_time($time){
		date_default_timezone_set('Asia/Chongqing');
		if($time=="today"){
			$user_id=$this->session->userdata('user_id');
			$this->db->where('user_id',$user_id);
			
			$tomorrow=date("Y-m-d",strtotime("+1 day"));
			$this->db->where('unix_timestamp(cal_time)<',strtotime($tomorrow));
			$this->db->where('unix_timestamp(cal_time)>',time());
			$this->db->where('active',1);
			$this->db->order_by('cal_time','asc');
			$query = $this->db->get('calendar');
			return $query->result();
		}
		elseif($time=="all"){
			$query=$this->get_future_cal();
			return $query;
		}	
		elseif ($time=="tomorrow"){
			$user_id=$this->session->userdata('user_id');
			$this->db->where('user_id',$user_id);
			
			$tomorrow=date("Y-m-d",strtotime("+1 day"));
			$day2=date("Y-m-d",strtotime("+2 day"));
			$this->db->where('unix_timestamp(cal_time)<',strtotime($day2));
			$this->db->where('unix_timestamp(cal_time)>',strtotime($tomorrow));
			$this->db->where('active',1);
			$this->db->order_by('cal_time','desc');
			$query = $this->db->get('calendar');
			return $query->result();
		}
		elseif ($time=="week"){
			$user_id=$this->session->userdata('user_id');
			$this->db->where('user_id',$user_id);
			
			$sunday=date("Y-m-d",strtotime("next Monday"));
			$this->db->where('unix_timestamp(cal_time)<',strtotime($sunday));
			$this->db->where('unix_timestamp(cal_time)>',time());
			$this->db->where('active',1);
			$this->db->order_by('cal_time','desc');
			$query = $this->db->get('calendar');
			return $query->result();
			
		}
	}
	
}